import 'package:flutter/material.dart';
import 'package:x3_base_core/x3_base_core.dart';
import 'package:x3_component_core/utils/base_component_utils.dart';

/// 显示手机号码 可隐藏小部件
class CellphoneNumberWidget extends StatelessWidget {
  /// 手机号码
  final String numBer;

  /// 是否隐藏
  final bool isHide;

  /// 文本样式
  final TextStyle style;

  /// 显示前缀
  final String prefix;

  /// 前缀样式
  final TextStyle prefixStyle;

  const CellphoneNumberWidget({Key key, @required this.numBer, this.isHide = true, this.style, this.prefix, this.prefixStyle}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    if (numBer.length != 11) return SizedBox();
    String before = numBer.substring(0, 3);
    String after = numBer.substring(7);
    TextStyle textStyle = style != null ? style : Theme.of(context).textTheme.subtitle2;
    TextStyle textPrefixStyle = prefixStyle != null
        ? prefixStyle
        : style != null
            ? style
            : Theme.of(context).textTheme.subtitle2;
    if (prefix != null)
      return Row(
        mainAxisSize: MainAxisSize.min,
        children: [
          if (isHide) BaseComponentUtils.buildTextSpanWidget([prefix, "$before****$after"], styles: {0: textPrefixStyle, 1: textStyle}),
          if (!isHide) BaseComponentUtils.buildTextSpanWidget([prefix, numBer], styles: {0: textPrefixStyle, 1: textStyle}),
          SizedBox(width: 8),
          InkWell(onTap: () => _copyEvent(), child: Icon(BaseIconData.x3Copy, size: 16)),
        ],
      );
    return Row(
      mainAxisSize: MainAxisSize.min,
      children: [
        if (isHide) Text("$before****$after", style: textStyle),
        if (!isHide) Text(numBer, style: textStyle),
        SizedBox(width: 8),
        InkWell(onTap: () => _copyEvent(), child: Icon(BaseIconData.x3Copy, size: 16)),
      ],
    );
  }

  void _copyEvent() {
    BaseClipboardTool.setDataToast(numBer);
  }
}
